#include <iostream>
#include <map>

using namespace std;


int func(int * array, int len);

typedef std::map<int, int> IntDict;


int main()
{
    int arr[] = {5, 1, 1, 2, 4, 8};
    int ret = func(arr, int(sizeof(arr) / sizeof(int)));
    if (ret != -1)
    {
        cout << "duplicate value: " << ret << endl;
    }
}


void print_dict(IntDict dict)
{
    for (IntDict::iterator iter = dict.begin(); iter != dict.end(); iter ++)
    {
        cout << iter->first << ": " << iter->second << "\n";
    }
}


int func(int* array, int len)
{

    IntDict count_map;

    cout << "array len: " << len << endl;
    int* arr_ptr = array;
    int index = 0;
    while (true)
    {

        if (count_map.count(array[index]) == 0)
        {
            count_map[array[index]] = 1;
        }
        else
        {
            count_map[array[index]] += 1;
        }
        if (++index >= len)
            break;
    }

    print_dict(count_map);


    for (IntDict::iterator iter = count_map.begin(); iter != count_map.end(); iter ++)
    {
        if (iter->second == 2)
        {
            return iter->first;
        }
    }

    return -1; 
}